package com.maven.blog.interceptor;


import com.maven.blog.constant.Constant;
import com.maven.blog.utils.JwtsUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


// 拦截器
@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("LoginInterceptor ⽬标⽅法执⾏前执⾏..");

        String jwtToken = request.getHeader(Constant.USER_TOKEN_HEADER);
        log.info("token:{}",jwtToken);

        Claims claims = JwtsUtils.parseJwt(jwtToken);
        if(claims != null ){
            log.info("令牌验证通过, 放⾏");
            return true;
        }
        response.setStatus(401);
        return true;
    }

}
